home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / xulrunner-1.9.0.14 / chrome / cview.jar / content / cview / cview-rdf.js < prev    next >
Encoding:
Text File  |  2004-04-18  |  7.1 KB  |  251 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  *
  3.  * ***** BEGIN LICENSE BLOCK *****
  4.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5.  *
  6.  * The contents of this file are subject to the Mozilla Public License Version
  7.  * 1.1 (the "License"); you may not use this file except in compliance with
  8.  * the License. You may obtain a copy of the License at
  9.  * http://www.mozilla.org/MPL/
  10.  *
  11.  * Software distributed under the License is distributed on an "AS IS" basis,
  12.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13.  * for the specific language governing rights and limitations under the
  14.  * License.
  15.  *
  16.  * The Original Code is Chatzilla.
  17.  *
  18.  * The Initial Developer of the Original Code is
  19.  * New Dimensions Consulting, Inc.
  20.  * Portions created by the Initial Developer are Copyright (C) 1999
  21.  * the Initial Developer. All Rights Reserved.
  22.  *
  23.  * Contributor(s):
  24.  *   Robert Ginda, rginda@ndcico.com, original author
  25.  *
  26.  * Alternatively, the contents of this file may be used under the terms of
  27.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  28.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29.  * in which case the provisions of the GPL or the LGPL are applicable instead
  30.  * of those above. If you wish to allow use of your version of this file only
  31.  * under the terms of either the GPL or the LGPL, and not to allow others to
  32.  * use your version of this file under the terms of the MPL, indicate your
  33.  * decision by deleting the provisions above and replace them with the notice
  34.  * and other provisions required by the GPL or the LGPL. If you do not delete
  35.  * the provisions above, a recipient may use your version of this file under
  36.  * the terms of any one of the MPL, the GPL or the LGPL.
  37.  *
  38.  * ***** END LICENSE BLOCK ***** */
  39.  
  40. /*
  41.  * This file is largely a ripoff of the chatzilla rdf.js file.  It could (and
  42.  * probably should) be refactored into a more reusable library, and placed in
  43.  * some global place (like utils.js)
  44.  */
  45.  
  46. /*
  47.  * The RDFHelper class wraps useful the RDFService and in-memory-datasource
  48.  * into a single object making them generally easier to use, and defines
  49.  * some RDF resources and literals that will be used throughout the app.
  50.  * it also makes it wasy to debug RDF asserts, (by forwarding
  51.  * RDFHelper.prototype.Assert() to RDFHelper.prototype.dAssert().)
  52.  */
  53.  
  54. const RES_PFX = "http://www.mozilla.org/NC-xpcom#";
  55.  
  56. const nsIRDFResource = Components.interfaces.nsIRDFResource;
  57. const nsIRDFNode = Components.interfaces.nsIRDFNode;
  58. const nsIRDFLiteral = Components.interfaces.nsIRDFLiteral;
  59.  
  60. function RDFHelper()
  61. {
  62.     const RDF_MEMORYDS_CONTRACTID =
  63.         "@mozilla.org/rdf/datasource;1?name=in-memory-datasource";
  64.     const RDF_DS_IID = Components.interfaces.nsIRDFDataSource;
  65.  
  66.     const RDF_DS_CONTRACTID = "@mozilla.org/rdf/rdf-service;1";
  67.     const RDF_SVC_IID = Components.interfaces.nsIRDFService;
  68.  
  69.     this.ds =
  70.         Components.classes[RDF_MEMORYDS_CONTRACTID].createInstance(RDF_DS_IID);
  71.     this.svc = 
  72.         Components.classes[RDF_DS_CONTRACTID].getService(RDF_SVC_IID);    
  73.  
  74.     /* predefined nodes */
  75.     this.resRoot = this.svc.GetResource ("NC:cview-data");
  76.     this.resEmptyRoot = this.svc.GetResource ("NC:cview-data-empty");
  77.  
  78.     /* predefined arcs */
  79.     this.resCmp       = this.svc.GetResource (RES_PFX + "component");
  80.     this.resContractID    = this.svc.GetResource (RES_PFX + "contractid");
  81.     this.resCLSID     = this.svc.GetResource (RES_PFX + "clsid");
  82.     this.resIfc       = this.svc.GetResource (RES_PFX + "interface");
  83.     this.resIName     = this.svc.GetResource (RES_PFX + "iname");
  84.     this.resIID       = this.svc.GetResource (RES_PFX + "iid");
  85.     this.resShow      = this.svc.GetResource (RES_PFX + "show");
  86.  
  87.     /* predefined literals */
  88.     this.litTrue  = this.svc.GetLiteral ("true");
  89.     this.litFalse = this.svc.GetLiteral ("false");
  90.  
  91. }
  92.  
  93. RDFHelper.prototype.GetResource =
  94. function rdf_getr (s)
  95. {
  96.     return this.svc.GetResource(s);
  97. }
  98.  
  99. RDFHelper.prototype.GetLiteral =
  100. function rdf_getl (s)
  101. {
  102.     if (!s)
  103.     {
  104.         dd ("rdf_getl: no argument provided!");
  105.         dd (getStackTrace());
  106.         
  107.         return "";
  108.     }
  109.     
  110.     return this.svc.GetLiteral(s);
  111. }
  112.  
  113. RDFHelper.prototype.Assert =
  114. function rdf_assert (n1, a, n2, f)
  115. {
  116.     if (typeof f == "undefined")
  117.         f = true;
  118.  
  119. //    return this.dAssert (n1, a, n2, f);
  120.     return this.ds.Assert (n1, a, n2, f);
  121. }
  122.  
  123. RDFHelper.prototype.Unassert =
  124. function rdf_uassert (n1, a, n2)
  125. {
  126.     /*return this.dUnassert (n1, a, n2);*/
  127.     return this.ds.Unassert (n1, a, n2);
  128. }
  129.  
  130. RDFHelper.prototype.dAssert =
  131. function rdf_dassert (n1, a, n2, f)
  132. {
  133.     var n1v = n1 ? n1.Value : "!!!";
  134.     var av = a ? a.Value : "!!!";
  135.     var n2v = n2 ? n2.Value : "!!!";
  136.     
  137.     if (!n1 || !a || !n2)
  138.     {
  139.         dd ("n1 " + n1v + " a " + av + " n2 " + n2v);
  140.         dd(getStackTrace());
  141.     }
  142.     
  143.     this.ds.Assert (n1, a, n2, f)
  144. }
  145.  
  146. RDFHelper.prototype.dUnassert =
  147. function rdf_duassert (n1, a, n2)
  148. {
  149.  
  150.     var n1v = n1 ? n1.Value : "!!!";
  151.     var av = a ? a.Value : "!!!";
  152.     var n2v = n2 ? n2.Value : "!!!";
  153.     
  154.     if (!n1 || !a || !n2)
  155.         dd(getStackTrace());
  156.     
  157.     this.ds.Unassert (n1, a, n2)
  158.  
  159. }
  160.  
  161. RDFHelper.prototype.Change =
  162. function rdf_duassert (n1, a, n2)
  163. {
  164.  
  165.     var oldN2 = this.ds.GetTarget (n1, a, true);
  166.     if (!oldN2)
  167.     {
  168.         dd ("** Unable to change " + n1.Value + " -[" + a.Value + "]->, " +
  169.             "because old value was not found.");
  170.         return;
  171.     }
  172.     
  173.     this.ds.Change (n1, a, oldN2, n2);
  174.     
  175. }
  176.  
  177. RDFHelper.prototype.GetTarget =
  178. function rdf_gettarget (n1, a)
  179. {
  180.     return this.ds.GetTarget (n1, a, true);
  181. }
  182.  
  183. RDFHelper.prototype.GetTargets =
  184. function rdf_gettarget (n1, a)
  185. {
  186.     return this.ds.GetTargets (n1, a, true);
  187. }
  188.  
  189. RDFHelper.prototype.clearTargets =
  190. function rdf_inittree (n1, a, recurse)
  191. {
  192.     if (typeof recurse == "undefined")
  193.         recurse = false;
  194.  
  195.     var targets = this.ds.GetTargets(n1, a, true);
  196.  
  197.     while (targets.hasMoreElements())
  198.     {
  199.         var n2 = targets.getNext().QueryInterface(nsIRDFNode);
  200.  
  201.         if (recurse)
  202.         {
  203.             try
  204.             {
  205.                 var resN2 = n2.QueryInterface(nsIRDFResource);
  206.                 var arcs = this.ds.ArcLabelsOut(resN2);
  207.  
  208.                 while (arcs.hasMoreElements())
  209.                 {
  210.                     arc = arcs.getNext().QueryInterface(nsIRDFNode);
  211.                     this.clearTargets (resN2, arc, true);
  212.                 }
  213.             }
  214.             catch (e)
  215.             {
  216.                 /*
  217.                 dd ("** Caught " + e + " while recursivley clearing " +
  218.                     n2.Value + " **");
  219.                 */
  220.             }
  221.         }
  222.         
  223.         this.Unassert (n1, a, n2);
  224.     }
  225. }    
  226.  
  227. RDFHelper.prototype.initTree =
  228. function rdf_inittree (id)
  229. {
  230.     var tree = document.getElementById (id);
  231.     tree.database.AddDataSource (this.ds);
  232. }
  233.  
  234. RDFHelper.prototype.setTreeRoot =
  235. function rdf_settroot (id, root)
  236. {
  237.     var tree = document.getElementById (id);
  238.  
  239.     if (typeof root == "object")
  240.         root = root.Value;
  241.     tree.setAttribute ("ref", root);
  242. }
  243.  
  244. RDFHelper.prototype.getTreeRoot =
  245. function rdf_gettroot (id, root)
  246. {
  247.     var tree = document.getElementById (id);
  248.  
  249.     return tree.getAttribute ("ref");
  250. }
  251.